<?php

class View extends CComponent {
	/**
	 * @var array the breadcrumbs of the current page. The value of this property will
	 * be assigned to {@link CBreadcrumbs::links}. Please refer to {@link CBreadcrumbs::links}
	 * for more details on how to specify this property.
	 */
	public $breadcrumbs = array();
	public $viewName = '';
	protected $_viewData = array();
	
	/**
	 * @var string the default layout for the controller view. Defaults to '//layouts/column1',
	 * meaning using a single column layout. See 'protected/views/layouts/column1.php'.
	 */
	protected $_layout;
	public $operates = array();
	protected $_controller = null;
	
	public function setController(Controller $controller) {
		$this->_controller = $controller;
	}
	
	public function init() {
		
	}
	
	public function configrue($config) {
		foreach($config as $k=>$v) {
			$this->$k = $v;
		}
	}
	
	public function addViewData($name, $value = null) {
		$data = is_array($name) ? $name : array($name=>$value);
		foreach($data as $k=>$v) {
			$this->_viewData[$k] = $v;
		}
	}
	
	public function removeViewData($name) {
		$keys = is_array($name) ? $name : array($name);
		foreach($keys as $key) {
			unset($this->_viewData[$key]);
		}
	}
	
	public function getViewData() {
		return $this->_viewData;
	}
	
	public function setViewData($data) {
		$this->_viewData = $data;
	}
	
	public function render($view = null, $data = array()) {
		if($this->beforeRender()) {
			$viewName = $view ? $view : $this->viewName;
			$this->addViewData($data);
			$this->_controller->layout = $this->getLayout();
			$this->_controller->render(
				$viewName,
				$this->getViewData()
			);
		} else {
			return false;
		}
		
	}
	
	protected function beforeRender() {
		return true;
	}
	
	public function getLayout() {
		return $this->_layout ? $this->_layout : $this->_controller->layout;
	}
	
	public function setLayout($layout) {
		$this->_layout = $layout;
	}
	
}

?>